package com.ruge.controller;

import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.Deployment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

@RestController
public class ActivitiController {

    @Resource
    private RepositoryService repositoryService;

    // activiti/oa-01.bpmn20.xml
    @GetMapping("deployProcess")
    public Map<String, Object> deployProcess(@RequestParam("resourceName") String resourceName) {

        Map<String, Object> result = new HashMap<>();

        InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(resourceName);
        Deployment deployment = repositoryService.createDeployment()
                .addInputStream(resourceName, inputStream)
                .name("My Process Deployment")
                .deploy();
        System.out.println("部署ID: " + deployment.getId());
        System.out.println("部署名称: " + deployment.getName());
        result.put("msg", "操作成功");
        result.put("data", deployment);
        return result;
    }

}
